package com.itranlin.voddemo.common.aspect;

import com.itranlin.voddemo.common.annotation.GetVodClient;
import com.itranlin.voddemo.utils.VodClientUtils;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;


@Aspect
@Component
public class GetVodClientAspect implements HandlerMethodArgumentResolver {

    private VodClientUtils vodClientUtils;

    public GetVodClientAspect(VodClientUtils vodClientUtils) {
        this.vodClientUtils = vodClientUtils;
    }

    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.hasParameterAnnotation(GetVodClient.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        return vodClientUtils.getVodClient();
    }

}
